<?php

/**
 * @file
 * Home of the XaFeedsHTTPFetcher and related classes.
 */

/**
 * Fetches data via HTTP.
 */
class XaFeedsHTTPFetcher extends FeedsHTTPFetcher {

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    $defaults = parent::configDefaults();
    $defaults['one_time_source'] = FALSE;
    return $defaults;
  }

  /**
   * @Override parent::configForm().
   */
  public function configForm(&$form_state) {
    $form = parent::configForm($form_state);

    $form['advanced']['one_time_source'] = array(
      '#type'          => 'checkbox',
      '#title'         => t('One time source'),
      '#description'   => t('Enabled to skip import of this feed item if it has been imported already.'),
      '#default_value' => $this->config['one_time_source'],
    );

    return $form;
  }

  /**
   * Override parent::importPeriod().
   */
  public function importPeriod(FeedsSource $source) {
    /*
     * @see scheduleImport() in FeedsSource.inc line #296 for the idea.
     */
    if ($this->config['one_time_source']) {
      // skip import of this feed item if imported timestamp isn't null.
      // @todo: check if the previous import is successful.
      if ($source->imported > 0 && $source->itemCount() > 0) {
        return FEEDS_SCHEDULE_NEVER;
      }
    }

    return parent::importPeriod($source);
  }

}